package org.jeecg.modules.fxjdh.service.impl;

import org.jeecg.modules.fxjdh.entity.FxjDh08;
import org.jeecg.modules.fxjdh.entity.FxjDh0810;
import org.jeecg.modules.fxjdh.mapper.FxjDh0810Mapper;
import org.jeecg.modules.fxjdh.mapper.FxjDh08Mapper;
import org.jeecg.modules.fxjdh.service.IFxjDh08Service;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 微信催收
 * @Author: zhaodui
 * @Date:   2020-02-22
 * @Version: V1.0
 */
@Service
public class FxjDh08ServiceImpl extends ServiceImpl<FxjDh08Mapper, FxjDh08> implements IFxjDh08Service {

	@Autowired
	private FxjDh08Mapper fxjDh08Mapper;
	@Autowired
	private FxjDh0810Mapper fxjDh0810Mapper;
	
	@Override
	@Transactional
	public void saveMain(FxjDh08 fxjDh08, List<FxjDh0810> fxjDh0810List) {
		fxjDh08Mapper.insert(fxjDh08);
		if(fxjDh0810List!=null && fxjDh0810List.size()>0) {
			for(FxjDh0810 entity:fxjDh0810List) {
				//外键设置
				entity.setQuery01(fxjDh08.getQuery01());
				fxjDh0810Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FxjDh08 fxjDh08,List<FxjDh0810> fxjDh0810List) {
		fxjDh08Mapper.updateById(fxjDh08);
		
		//1.先删除子表数据
		fxjDh0810Mapper.deleteByMainId(fxjDh08.getId());
		
		//2.子表数据重新插入
		if(fxjDh0810List!=null && fxjDh0810List.size()>0) {
			for(FxjDh0810 entity:fxjDh0810List) {
				//外键设置
				entity.setQuery01(fxjDh08.getQuery01());
				fxjDh0810Mapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		fxjDh0810Mapper.deleteByMainId(id);
		fxjDh08Mapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fxjDh0810Mapper.deleteByMainId(id.toString());
			fxjDh08Mapper.deleteById(id);
		}
	}
	
}
